<template>
  <a-modal
    :title="title"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="用户名">
          <a-input v-decorator="['name', {rules: [{required: true, min: 2, message: '请至少输入两个字符'}]}]" />
        </a-form-item>
        <a-form-item label="角色">
          <a-select
            show-search
            placeholder="input search text"
            :default-active-first-option="false"
            :show-arrow="false"
            :filter-option="false"
            :not-found-content="null"
            @search="handleSearch"
            @change="handleChange"
            v-decorator="['role_id', {rules: [{required: true}]}]"
          >
            <a-select-option v-for="d in selectData" :key="d.id">
              {{ d.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="邮箱">
          <a-input v-decorator="['email', {rules: [{required: true}]}]" />
        </a-form-item>
        <a-form-item label="密码" v-if="edit">
          <a-input size="large" type="password" placeholder="密码" v-decorator="['password', checkRules.password]">
            <a-icon slot="prefix" type="lock" />
          </a-input>
        </a-form-item>
        <a-form-item label="确认密码" v-if="edit">
          <a-input size="large" type="password" placeholder="确认密码" v-decorator="['password_confirmation', checkRules.confirmPassword]">
            <a-icon slot="prefix" type="lock" />
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-input type="hidden" v-decorator="['id']">
          </a-input>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { roleList } from '@/api/role'
// 表单字段
const fields = ['name', 'email', 'id', 'role_id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    title: {
      type: String,
      default: () => ''
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      edit: true,
      form: this.$form.createForm(this),
      selectData: [],
      checkRules: {
        password: {
          rules: [
            {
              required: true,
              validator: (rule, value, cbfn) => {
                if (!value) {
                  cbfn('请输入密码')
                }
                cbfn()
              }
            }
          ],
          validateTrigger: 'change'
        },
        confirmPassword: {
          rules: [
            {
              required: true,
              validator: (rule, value, cbfn) => {
                if (!value) {
                  cbfn('请再一次输入密码')
                } else if (value && value !== this.form.getFieldValue('password')) {
                  cbfn('两次密码不一致')
                }
                cbfn()
              }
            }
          ],
          validateTrigger: 'change'
        }
      }
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  watch: {
    title: function (val) {
      if (val === '编辑用户') {
        this.edit = false
      } else {
        this.edit = true
      }
    }
  },
  mounted () {
    this.getRoleList({ 'page_size': 100 })
  },
  methods: {
    getRoleList (filter = {}) {
        roleList(filter).then((res) => {
          if (res.code !== 0) {
            this.$message.error('请求角色报错' + res.message)
          } else {
            this.selectData = res.data.data
            console.log(this.selectData)
          }
        }).catch((error) => {
          this.$message.error('请求角色报错' + error)
        })
    },
    handleSearch (value) {
    },
    handleChange (value) {
    }
  }
}
</script>
